package cn.impl;

import cn.annotation.Log;
import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.LogOperType;
import cn.dao.EquipmentDAO;
import cn.model.TEquipment;
import cn.model.VEquipmentClassRoom;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("equipmentdao")
public class EquipmentDAOImpl implements EquipmentDAO {

    //连接数据库操作层接口,实现业务与数据的交换
    private iHibBaseDAO bdao= null;

    public EquipmentDAOImpl(){
        bdao = new HibBaseDAOImpl();
    }

    /**
     * 添加一个设备
     *
     * @param equipment 对象
     * @return 添加成功返回受影响的行数
     */
    @Log(isSaveLog=true,operationType= LogOperType.ADD,operationName="添加一条设备信息")
    @Override
    public int addEquipment(TEquipment equipment) {
        return (Integer) bdao.insert(equipment);
    }

    /**
     * 删除
     *
     * @param equipmentid 编号
     * @return 添加成功返回受影响的行数
     */
    @Log(isSaveLog=true,operationType= LogOperType.DELETE,operationName="删除一条设备信息")
    @Override
    public boolean deleteEquipment(int equipmentid) {
        return bdao.delete(equipmentid);
    }

    /**
     * 修改
     *
     * @param equipment 需要修改的对象
     * @return
     */
    @Log(isSaveLog=true,operationType= LogOperType.MODIFY,operationName="修改一条设备信息")
    @Override
    public boolean modifyEquipment(TEquipment equipment) {
        return bdao.update(equipment);
    }

    /**
     * 根据条件获取用户的列表
     *
     * @param wherecondition 组合查询条件字符串,
     * @param currentPage    按分页查询的当前页
     * @param pageSize       按分页查询的每页数量
     * @return List
     */
    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询设备信息列表")
    @Override
    public List<VEquipmentClassRoom> getEquipmentList(String wherecondition, int currentPage, int pageSize) {
        String hql = "from VEquipmentClassRoom";
        if (wherecondition!=null && !wherecondition.equals("")){
            hql +=wherecondition;
        }
        if (wherecondition==null && wherecondition.equals("")){
            hql += "order by createtime desc";
        }
        return bdao.selectByPage(hql,currentPage,pageSize);
    }

    /**
     * 根据条件获取符合条件的设备的数量
     *
     * @param wherecondition
     * @return
     */
    @Override
    public int getEquipmentAmount(String wherecondition) {
        String hql = "select count(*) from VEquipmentClassRoom";
        if (wherecondition!=null && !wherecondition.equals("")){
            hql +=wherecondition;
        }
        if (wherecondition==null && wherecondition.equals("")){
            hql += "order by createtime desc";
        }
        return bdao.selectValue(hql);
    }

    @Override
    public List<TEquipment> getAllEquipment() {
        String hql ="from TEquipment";
        return bdao.select(hql);
    }


    /**
     * 通过用户ID查询
     *
     * @param equipmentid 设备ID
     * @return 返回设备信息列表
     */
    @Override
    public VEquipmentClassRoom getVEquipmentRoleById(int equipmentid) {
        return (VEquipmentClassRoom)bdao.findById(VEquipmentClassRoom.class,equipmentid);
    }
}
